package ca.nfb.media.nfbPlayer.views
{
	import ca.nfb.media.nfbPlayer.ComModule;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Timer;
	import ca.turbulent.media.events.PyroEvent;
	import flash.events.FullScreenEvent;
	
	public class MessageView extends NFBPlayerView
	{
		private var timeOutTimer		:Timer = new Timer(7500, 1);
		private var messages			:Object = new Object();
		private var currentMessage		:String = "";
		
		
		public function MessageView()
		{
			super();
			this.visible = false;
			this.mouseEnabled = false;
			this.mouseChildren = false;
			TextField(message_txt).autoSize = TextFieldAutoSize.LEFT;
			timeOutTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timedOut, false, 0, true);
			MovieClip(background_mc).mouseEnabled = true;
			ComModule.getInstance().addEventListener(ComModule.MESSAGES_RECEIVED_EVENT, messagesReceived);
			
		}
		
		private function messagesReceived(evt:Event):void
		{
			messages = ComModule.getInstance().latestMessages;
			if (messages['Start'] != "") 
			{
				currentMessage = messages['Start'];
				if (messages['Warning'] != "")
					currentMessage += "<br>"+messages['Warning'];
					
				show();	
			}
			else
			{
				if (messages['Warning'] != "")
				{
					currentMessage = messages['Warning'];
					show();
				}
				
			}
		}
		
		override protected function streamStarted(evt:PyroEvent):void
		{
			if (messages['End'] != "")
				player.registerProgressCallback({name:this.name, funct:onProgress});
			
		}
	
		public function onProgress():void
		{
			if (this.messages['End'] != "" && this.currentMessage != this.messages['End'])
			{	
				if ((player.time > 0 && player.duration > 0) && player.time >= player.duration-10)
				{
					currentMessage = messages['End'];
					// player.unregisterProgressCallback({name:this.name, funct:onProgress});
					show();
				}
			}				
		}
		
		override protected function stageResized(evt:Event=null):void
		{
			background_mc.width = stage.stageWidth -13;
			background_mc.height = stage.stageHeight -60;
			message_txt.width = background_mc.width - 14;
			message_txt.height = background_mc.height - 14;
			message_txt.x = 7;
			message_txt.y = 7;
			this.x = (stage.stageWidth - this.width) / 2;
			this.y = ((stage.stageHeight-core.controllerHeight) - this.height) / 2;
		}
		
		override protected function fullscreenHandler(evt:FullScreenEvent):void
		{
			background_mc.width = stage.stageWidth -13;
			background_mc.height = stage.stageHeight -60;
			message_txt.width = background_mc.width - 14;
			message_txt.height = background_mc.height - 14;
			message_txt.x = 7;
			message_txt.y = 7;
			this.x = (stage.stageWidth - this.width) / 2;
			this.y = ((stage.stageHeight-core.controllerHeight) - this.height) / 2;
		}
		
		private function show():void
		{
			stageResized();
			
			if (timeOutTimer.running)
				timeOutTimer.stop();
		
			this.message_txt.htmlText = this.currentMessage;
			timeOutTimer.start();
			this.visible = true;
		}
		
		public function timedOut(evt:TimerEvent):void
		{
			this.visible = false;
			this.message_txt.htmlText = "";
		}
		
	}
}